home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / DA / P / PCalculator.cpt / CalcDA sources / CalcDA_Buttons.c < prev    next >
Text File  |  1990-07-06  |  12KB  |  362 lines

  1. /*
  2.  * CalcDA_Buttons.c for the Programmer's Calculator project 
  3.  * 
  4.  * Copyright 1990, Peter Ohler 
  5.  * 
  6.  * All Rights Reserved 
  7.  * 
  8.  * The Programmer's Calculator and the source code are shareware.  That means
  9.  * they are not free.  If you use either the source or the calculator then
  10.  * send $5 or $10 (whatever you feel its worth) to the address that follows.
  11.  * The source and calculator can be distributed for free.  Prior to any sale
  12.  * of either the source code or the calculator my permission must be
  13.  * obtained.  This includes sales by shareware distribution houses that sell
  14.  * shareware.  
  15.  * 
  16.  * Peter Ohler 
  17.  * 
  18.  * 3184 Rohrer Drive, Lafayette CA 94549 
  19.  * 
  20.  * (415) 284-7828 
  21.  * 
  22.  * ***************************************************************************
  23.  * 
  24.  * This file contains all the button definitions, including the button names,
  25.  * the key key to button mapping, and the functions to execute for each
  26.  * button.  Ideally (and initially) all the button names and functions should
  27.  * be in on C structure.  Unfortunately it is not possible to initialize
  28.  * pointers to functions at compile time for a DA.  Instead an ugly switch
  29.  * statement is used to execute the functions for each button and the button
  30.  * names are kept in an array.  At least it works.  
  31.  */
  32. #include <SANE.h>
  33. #include "CalcDA.h"
  34.  
  35. /*
  36.  * ***************************************************************************
  37.  * prototypes 
  38.  */
  39. extern void    UpdateXRegText(void);
  40.  
  41. extern int    ToggleRegs(void);
  42. extern int    ToggleStack(Stack *stack);
  43. extern int    DoIntDigit(int digit);
  44. extern int    DoDoubleDigit(int digit);
  45.  
  46. extern int    DelIntDigit(void);
  47. extern int    DelDoubleDigit(void);
  48.  
  49. extern int    ClearAllReg(void);
  50. extern int    ClearXReg(void);
  51.  
  52. extern int    CopyXreg(void);
  53. extern int    CopyToXreg(void);
  54. extern int    SwapXreg(void);
  55. extern int    AddX2Reg(void);
  56.  
  57. extern int    ChangeSign(void);
  58.  
  59. extern void    FreezeXReg(void);
  60. extern double    Power(double x, double y);
  61.  
  62. extern int    DoArithmetic(IntFunc func);
  63. extern int    DoEqual(void);
  64. extern int    DoOr(void);
  65. extern int    DoAnd(void);
  66. extern int    DoXor(void);
  67. extern int    DoShiftRight(void);
  68. extern int    DoShiftLeft(void);
  69. extern int    DoPlus(void);
  70. extern int    DoMinus(void);
  71. extern int    DoMultiply(void);
  72. extern int    DoDivide(void);
  73. extern int    DoModulo(void);
  74. extern int    DoPower(void);
  75. extern int    DoInvPower(void);
  76.  
  77. extern double    Log10(double x);
  78.  
  79. extern int    DoXFactorial(void);
  80.  
  81. extern int    DoTrig(DoubleFunc func, int inv);
  82. extern double    Sine(double x);
  83. extern double    Cosine(double x);
  84. extern double    Tangent(double x);
  85. extern double    ASin(double x);
  86. extern double    ACos(double x);
  87. extern double    ArcTangent(double x);
  88.  
  89. extern int    DoAscii(void);
  90. extern int    DoPrecision(void);
  91.  
  92. int    DoButton(int n);
  93.  
  94. /*
  95.  * ***************************************************************************
  96.  * variables 
  97.  */
  98. extern double    OneEighty;
  99. extern double    One;
  100.  
  101. extern int    exponentActive;
  102. extern int    afterDigits;
  103.  
  104. double    PI = 3.14159265358979323846;
  105.  
  106. /*
  107.  * The key map table for the integer mode of the calculator.  A value of 255
  108.  * indicates there is no mapping.  Other values are the button numbers.  A
  109.  * button number is (row * number of columns + column) * 2 + (1 if in the
  110.  * double mode, 0 if in the integer mode).  
  111.  */
  112. unsigned char    iKeyButtonMap[256] = {
  113.     255,    255,    255,    255,    255,    255,    255,    255, /*   0 */
  114.     46,    255,    255,    255,    255,    255,    255,    255,
  115.     255,    255,    255,    255,    255,    255,    255,    255, /*  16 */
  116.     255,    255,    255,    5,    255,    255,    255,    255,
  117.     255,    255,    255,    255,    255,    33,    11,    255, /*  32 */
  118.     255,    255,    31,    38,    255,    39,    255,    32,
  119.     42,    35,    36,    37,    28,    29,    30,    21,  /*  48 */
  120.     22,    23,    255,    255,    255,    48,    255,    255,
  121.     255,    14,    15,    16,    7,    8,    9,    255, /*  64 */
  122.     255,    255,    255,    255,    255,    255,    255,    255,
  123.     255,    255,    255,    255,    255,    255,    255,    255, /*  80 */
  124.     255,    255,    255,    255,    255,    255,    12,    255,
  125.     255,    14,    15,    16,    7,    8,    9,    255, /*  96 */
  126.     255,    255,    255,    255,    255,    255,    255,    255,
  127.     255,    255,    255,    255,    255,    255,    255,    255, /* 112 */
  128.     255,    255,    255,    255,    10,    255,    19,    255,
  129.     255,    255,    255,    255,    255,    255,    255,    255, /* 128 */
  130.     255,    255,    255,    255,    255,    255,    255,    255,
  131.     255,    255,    255,    255,    255,    255,    255,    255, /* 144 */
  132.     255,    255,    255,    255,    255,    255,    255,    255,
  133.     255,    255,    255,    255,    255,    255,    255,    255, /* 160 */
  134.     255,    255,    255,    255,    255,    255,    255,    255,
  135.     255,    40,    255,    255,    255,    255,    255,    255, /* 176 */
  136.     255,    255,    255,    255,    255,    255,    255,    255,
  137.     255,    255,    255,    255,    255,    255,    255,    255, /* 192 */
  138.     255,    255,    255,    255,    255,    255,    255,    255,
  139.     255,    255,    255,    255,    255,    255,    255,    255, /* 208 */
  140.     255,    255,    255,    255,    255,    255,    255,    255,
  141.     255,    255,    255,    255,    255,    255,    255,    255, /* 224 */
  142.     255,    255,    255,    255,    255,    255,    255,    255,
  143.     255,    255,    255,    255,    255,    255,    255,    255, /* 240 */
  144.     255,    255,    255,    255,    255,    255,    255,    255
  145. };
  146.  
  147. /*
  148.  * The real or double mode keymap 
  149.  */
  150. unsigned char    rKeyButtonMap[256] = {
  151.     255,    255,    255,    255,    255,    255,    255,    255, /*   0 */
  152.     46,    255,    255,    255,    255,    255,    255,    255,
  153.     255,    255,    255,    255,    255,    255,    255,    255, /*  16 */
  154.     255,    255,    255,    5,    255,    255,    255,    255,
  155.     255,    255,    255,    255,    255,    255,    255,    255, /*  32 */
  156.     255,    255,    31,    38,    255,    39,    43,    32,
  157.     42,    35,    36,    37,    28,    29,    30,    21,  /*  48 */
  158.     22,    23,    255,    255,    255,    48,    255,    255,
  159.     255,    255,    255,    255,    255,    44,    255,    255, /*  64 */
  160.     255,    255,    255,    255,    255,    255,    255,    255,
  161.     255,    255,    255,    255,    255,    255,    255,    255, /*  80 */
  162.     255,    255,    255,    255,    255,    255,    255,    255,
  163.     255,    255,    255,    255,    255,    44,    255,    255, /*  96 */
  164.     255,    255,    255,    255,    255,    255,    255,    255,
  165.     255,    255,    255,    255,    255,    255,    255,    255, /* 112 */
  166.     255,    255,    255,    255,    255,    255,    255,    255,
  167.     255,    255,    255,    255,    255,    255,    255,    255, /* 128 */
  168.     255,    255,    255,    255,    255,    255,    255,    255,
  169.     255,    255,    255,    255,    255,    255,    255,    255, /* 144 */
  170.     255,    255,    255,    255,    255,    255,    255,    255,
  171.     255,    255,    255,    255,    255,    255,    255,    255, /* 160 */
  172.     255,    255,    255,    255,    255,    255,    255,    255,
  173.     255,    40,    255,    255,    255,    255,    255,    255, /* 176 */
  174.     45,    45,    255,    255,    255,    255,    255,    255,
  175.     255,    255,    255,    255,    255,    255,    255,    255, /* 192 */
  176.     255,    255,    255,    255,    255,    255,    255,    255,
  177.     255,    255,    255,    255,    255,    255,    255,    255, /* 208 */
  178.     255,    255,    255,    255,    255,    255,    255,    255,
  179.     255,    255,    255,    255,    255,    255,    255,    255, /* 224 */
  180.     255,    255,    255,    255,    255,    255,    255,    255,
  181.     255,    255,    255,    255,    255,    255,    255,    255, /* 240 */
  182.     255,    255,    255,    255,    255,    255,    255,    255
  183. };
  184.  
  185. /*
  186.  * These are all the button names.  The button number or index is the same as
  187.  * the key map arrays.  
  188.  */
  189. char    buttonNames[98][6] = {    "\pReg",    "\pReg",
  190.                 "\pType",    "\pType",
  191.                 "\pBase",    "\pNote",
  192.                 "\pSign",    "\prad/°",
  193.                 "\pASCII",    "\pPrec",
  194.                 "\pClr",    "\pClr",
  195.                 "\pClrx",    "\pClrx",
  196.                 "\pD",        "\psin",
  197.                 "\pE",        "\pcos",
  198.                 "\pF",        "\ptan",
  199.                 "\p|",        "\p°>rad",
  200.                 "\p&",        "\p10^x",
  201.                 "\p^",        "\plog",
  202.                 "\px^2",    "\px^2",
  203.                 "\pA",        "\pasin",
  204.                 "\pB",        "\pacos",
  205.                 "\pC",        "\patan",
  206.                 "\p<<",        "\prad>°",
  207.                 "\p>>",        "\pe^x",
  208.                 "\p~",        "\pln",
  209.                 "\p√x",        "\p√x",
  210.                 "\p7",        "\p7",
  211.                 "\p8",        "\p8",
  212.                 "\p9",        "\p9",
  213.                 "\px>>R",    "\px>>R",
  214.                 "\px<<R",    "\px<<R",
  215.                 "\px<>R",    "\px<>R",
  216.                 "\py^x",    "\py^x",
  217.                 "\p4",        "\p4",
  218.                 "\p5",        "\p5",
  219.                 "\p6",        "\p6",
  220.                 "\p*",        "\p*",
  221.                 "\p÷",        "\p÷",
  222.                 "\p%",        "\p1/x",
  223.                 "\px√y",    "\px√y",
  224.                 "\p1",        "\p1",
  225.                 "\p2",        "\p2",
  226.                 "\p3",        "\p3",
  227.                 "\p+",        "\p+",
  228.                 "\p-",        "\p-",
  229.                 "\p±",        "\p±",
  230.                 "\px!",        "\px!",
  231.                 "\p0",        "\p0",
  232.                 "\pFF",        "\p.",
  233.                 "\pFFFF",    "\pexp",
  234.                 "\pFFF…",    "\pπ",
  235.                 "\pdel",    "\pdel",
  236.                 "\pR+=x",    "\pR+=x",
  237.                 "\p=",        "\p="
  238.             };
  239.  
  240. /*
  241.  * ***************************************************************************
  242.  * functions 
  243.  */
  244. /*
  245.  * Just one large switch statement.  All functions return 0 if successful and
  246.  * non zero if an error occurs.  The return value of the function is the
  247.  * result code of the button function executed.  
  248.  */
  249. int
  250. DoButton(n)
  251.     int    n;
  252. {
  253.     int    ok = 0;
  254.     double    *dx = &xReg.value.dv;
  255.     long    *lx = &xReg.value.lv;
  256.  
  257.     switch (n * 2 + typeStack.pos) {
  258.     case 0:
  259.     case 1:        ok = ToggleRegs();                break;
  260.     case 2:
  261.     case 3:        ok = ToggleStack(&typeStack);            break;
  262.     case 4:        ok = ToggleStack(&baseStack);            break;
  263.     case 5:        ok = ToggleStack(¬eStack);            break;
  264.     case 6:        ok = ToggleStack(&signStack);            break;
  265.     case 7:        ok = ToggleStack(&angleStack);            break;
  266.     case 8:        ok = DoAscii();                    break;
  267.     case 9:        ok = DoPrecision();                break;
  268.     case 10:
  269.     case 11:    ok = ClearAllReg();                break;
  270.     case 12:
  271.     case 13:    ok = ClearXReg();                break;
  272.     case 14:    ok = DoIntDigit(0x0D);                break;
  273.     case 15:    ok = DoTrig(Sine, 0);                break;
  274.     case 16:    ok = DoIntDigit(0x0E);                break;
  275.     case 17:    ok = DoTrig(Cosine, 0);                break;
  276.     case 18:    ok = DoIntDigit(0x0F);                break;
  277.     case 19:    ok = DoTrig(Tangent, 0);            break;
  278.     case 20:    ok = DoArithmetic(DoOr);            break;
  279.     case 21:    *dx *= PI / OneEighty;        FreezeXReg();    break;
  280.     case 22:    ok = DoArithmetic(DoAnd);            break;
  281.     case 23:    *dx = Power(10.0, *dx);        FreezeXReg();    break;
  282.     case 24:    ok = DoArithmetic(DoXor);            break;
  283.     case 25:    *dx = Log10(*dx);        FreezeXReg();    break;
  284.     case 26:    *lx *= *lx;            FreezeXReg();    break;
  285.     case 27:    *dx *= *dx;            FreezeXReg();    break;
  286.     case 28:    ok = DoIntDigit(0x0A);                break;
  287.     case 29:    ok = DoTrig(ASin, 1);                break;
  288.     case 30:    ok = DoIntDigit(0x0B);                break;
  289.     case 31:    ok = DoTrig(ACos, 1);                break;
  290.     case 32:    ok = DoIntDigit(0x0C);                break;
  291.     case 33:    ok = DoTrig(ArcTangent, 1);            break;
  292.     case 34:    ok = DoArithmetic(DoShiftLeft);            break;
  293.     case 35:    *dx *= OneEighty / PI;        FreezeXReg();    break;
  294.     case 36:    ok = DoArithmetic(DoShiftRight);        break;
  295.     case 37:    *dx = Power(2.7182818284, *dx);    FreezeXReg();    break;
  296.     case 38:    *lx = ~*lx;            FreezeXReg();    break;
  297.     case 39:    *dx = log(*dx);            FreezeXReg();    break;
  298.     case 40:    *lx = sqrt((double)*lx);    FreezeXReg();    break;
  299.     case 41:    *dx = sqrt(*dx);        FreezeXReg();    break;
  300.     case 42:    ok = DoIntDigit(7);                break;
  301.     case 43:    ok = DoDoubleDigit(7);                break;
  302.     case 44:    ok = DoIntDigit(8);                break;
  303.     case 45:    ok = DoDoubleDigit(8);                break;
  304.     case 46:    ok = DoIntDigit(9);                break;
  305.     case 47:    ok = DoDoubleDigit(9);                break;
  306.     case 48:
  307.     case 49:    ok = CopyXreg();                break;
  308.     case 50:
  309.     case 51:    ok = CopyToXreg();                break;
  310.     case 52:
  311.     case 53:    ok = SwapXreg();                break;
  312.     case 54:
  313.     case 55:    ok = DoArithmetic(DoPower);            break;
  314.     case 56:    ok = DoIntDigit(4);                break;
  315.     case 57:    ok = DoDoubleDigit(4);                break;
  316.     case 58:    ok = DoIntDigit(5);                break;
  317.     case 59:    ok = DoDoubleDigit(5);                break;
  318.     case 60:    ok = DoIntDigit(6);                break;
  319.     case 61:    ok = DoDoubleDigit(6);                break;
  320.     case 62:
  321.     case 63:    ok = DoArithmetic(DoMultiply);            break;
  322.     case 64:
  323.     case 65:    ok = DoArithmetic(DoDivide);            break;
  324.     case 66:    ok = DoArithmetic(DoModulo);            break;
  325.     case 67:    *dx = One / *dx;        FreezeXReg();    break;
  326.     case 68:
  327.     case 69:    ok = DoArithmetic(DoInvPower);            break;
  328.     case 70:    ok = DoIntDigit(1);                break;
  329.     case 71:    ok = DoDoubleDigit(1);                break;
  330.     case 72:    ok = DoIntDigit(2);                break;
  331.     case 73:    ok = DoDoubleDigit(2);                break;
  332.     case 74:    ok = DoIntDigit(3);                break;
  333.     case 75:    ok = DoDoubleDigit(3);                break;
  334.     case 76:
  335.     case 77:    ok = DoArithmetic(DoPlus);            break;
  336.     case 78:
  337.     case 79:    ok = DoArithmetic(DoMinus);            break;
  338.     case 80:
  339.     case 81:    ok = ChangeSign();                break;
  340.     case 82:
  341.     case 83:    ok = DoXFactorial();                break;
  342.     case 84:    ok = DoIntDigit(0);                break;
  343.     case 85:    ok = DoDoubleDigit(0);                break;
  344.     case 86:    *lx = 0xFFL;            FreezeXReg();    break;
  345.     case 87:    afterDigits = 1;    UpdateXRegText();    break;
  346.     case 88:    *lx = 0xFFFFL;            FreezeXReg();    break;
  347.     case 89:    exponentActive = 1;    UpdateXRegText();    break;
  348.     case 90:    *lx = 0xFFFFFFFFL;        FreezeXReg();    break;
  349.     case 91:    *dx = PI;            FreezeXReg();    break;
  350.     case 92:    ok = DelIntDigit();                break;
  351.     case 93:    ok = DelDoubleDigit();                break;
  352.     case 94:
  353.     case 95:    ok =AddX2Reg();                    break;
  354.     case 96:
  355.     case 97:    ok = DoArithmetic(DoEqual);            break;
  356.     default:    ok = -1;                    break;
  357.     }
  358.     return ok;
  359. };
  360.  
  361.  
  362.